<template>
  <el-card :body-style="{padding: showMoreFilter ? '20px 20px 1px 20px': '20px'}">
    <el-form
      ref="topForm"
      :model="form"
      @submit.native.prevent
    >
      <div class="topsearch-container">
        <div :class="showMoreFilter?'topsearch-container-form':'topsearch-container-form topsearch-height'">
          <el-row :gutter="gutter">
            <el-col :span="$getSpan(layout, {xl: 6, lg: 8, md: 8, sm: 12, xs: 24})" class="item-height">
              <el-form-item prop="applyNo">
                <el-input v-model.trim="form.applyNo" clearable placeholder="发货申请编号/订单编号/PI编号" />
              </el-form-item>
            </el-col>
            <el-col :span="$getSpan(layout, {xl: 6, lg: 8, md: 8, sm: 12, xs: 24})" class="item-height">
              <el-form-item prop="companyNameForeign">
                <el-select
                  v-model="form.companyNameForeign"
                  placeholder="请输入客户名称"
                  clearable
                  remote
                  filterable
                  :remote-method="remoteCompany"
                  style="width: 100%"
                >
                  <el-option
                    v-for="item in companyList"
                    :key="item.companyNameForeign"
                    :label="item.companyNameForeign"
                    :value="item.companyNameForeign"
                  />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="$getSpan(layout, {xl: 6, lg: 8, md: 8, sm: 12, xs: 24})" class="item-height">
              <el-form-item prop="applyUser">
                <el-select
                  v-model="form.applyUser"
                  placeholder="请输入申请人名称"
                  clearable
                  remote
                  filterable
                  :remote-method="remoteApplyUser"
                  style="width: 100%"
                >
                  <el-option
                    v-for="(item,index) in applyUserList"
                    :key="index"
                    :label="item.accountName"
                    :value="item.accountName"
                  />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="$getSpan(layout, {xl: 6, lg: 8, md: 8, sm: 12, xs: 24})" class="item-height">
              <el-form-item prop="applyDate">
                <el-date-picker
                  v-model="form.applyDate"
                  type="daterange"
                  start-placeholder="起始申请日期"
                  end-placeholder="截止申请日期"
                  clearable
                  format="yyyy-MM-dd"
                  value-format="yyyy-MM-dd"
                  style="width: 100%;"
                />
              </el-form-item>
            </el-col>
            <span>
              <el-col :span="$getSpan(layout, {xl: 6, lg: 8, md: 8, sm: 12, xs: 24})" class="item-height">
                <el-form-item prop="deliverStatus">
                  <el-select
                    v-model="form.deliverStatus"
                    clearable
                    placeholder="请选择发货状态"
                    value=""
                    style="width: 100%;"
                  >
                    <el-option v-for="(item, index) in shipOptions" :key="index + item.value" :label="item.label" :value="item.value" />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="$getSpan(layout, {xl: 6, lg: 8, md: 8, sm: 12, xs: 24})" class="item-height">
                <el-form-item prop="paymentStatus">
                  <el-select
                    v-model="form.paymentStatus"
                    clearable
                    placeholder="请选择回款状态"
                    style="width: 100%"
                  >
                    <el-option v-for="(item, index) in moneyBackOptions" :key="index + item.value" :label="item.label" :value="item.value" />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="$getSpan(layout, {xl: 6, lg: 8, md: 8, sm: 12, xs: 24})" class="item-height">
                <el-form-item prop="approveStatus">
                  <el-select
                    v-model="form.approveStatus"
                    clearable
                    placeholder="请选择审批状态"
                    value=""
                    style="width: 100%;"
                  >
                    <el-option v-for="(item, index) in approvalOptions" :key="index + item.value" :label="item.label" :value="item.value" />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col
                v-if="form.approveStatus && form.approveStatus==='0'"
                :span="$getSpan(layout, {xl: 6, lg: 8, md: 8, sm: 12, xs: 24})"
                class="topsearch-item-height"
              >
                <el-form-item prop="myAudit">
                  <el-checkbox v-model="form.myAudit">待我审批</el-checkbox>
                  <el-button type="text" style="margin-left: 10px" @click="showMoreFilter = false">收起</el-button>
                </el-form-item>
              </el-col>
              <el-button v-if="showMoreFilter && form.approveStatus!=='0'" type="text" @click="showMoreFilter = false">收起</el-button>
            </span>
          </el-row>
        </div>
        <div class="topsearch-container-btn">
          <span style="display: flex; justify-content: flex-end;">
            <el-button v-if="!showMoreFilter" type="text" @click="showMoreFilter = true">更多筛选</el-button>
            <el-button type="primary" icon="el-icon-search" @click="submitForm('topForm')">查询</el-button>
            <el-button icon="el-icon-refresh-left" @click="resetForm('topForm')">重置</el-button>
          </span>
        </div>
      </div>
    </el-form>
  </el-card>
</template>

<script>
import { listApplyUserQuery, listCompanyNameQuery } from '@/service/customer'
import { mapGetters } from 'vuex'
export default {
  name: 'Topsearch',
  emit: ['submitData'],
  props: {
    value: {
      type: Object,
      default: () => {}
    },
    approvalOptions: {
      type: Array,
      required: true,
      default: () => []
    },
    shipOptions: {
      type: Array,
      required: true,
      default: () => []
    },
    moneyBackOptions: {
      type: Array,
      required: true,
      default: () => []
    }
  },
  data() {
    return {
      gutter: 16,
      showMoreFilter: false,
      companyList: [],
      applyUserList: []
    }
  },
  computed: {
    ...mapGetters(['layout']),
    buttonSpan() {
      return this.showMoreFilter ? 6 : 24
    },
    form: {
      get() {
        return this.value
      },
      set(val) {
        this.$emit('input', val)
      }
    }
  },
  watch: {
    'form.approveStatus': {
      handler(val) {
        if (val !== '0') {
          this.form.myAudit = false
        }
      }
    }
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$emit('submitData')
        } else {
          return false
        }
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
      this.form.myAudit = false
      this.$emit('submitData')
    },
    remoteCompany(query) {
      if (query.length < 100) {
        setTimeout(() => {
          this.listCompanyNameQueryHandle(query)
        }, 200)
      }
    },
    // 模糊查询客户名称
    async listCompanyNameQueryHandle(query) {
      const res = await listCompanyNameQuery({ companyNameForeign: query, pageNum: 1, pageSize: 500 })
      this.companyList = res.records || []
    },
    remoteApplyUser(query) {
      if (query.length < 100) {
        setTimeout(() => {
          this.listApplyUserQueryHandle(query)
        }, 200)
      }
    },
    async listApplyUserQueryHandle(query) {
      const res = await listApplyUserQuery({ accountName: query, pageNum: 1, pageSize: 500 })
      this.applyUserList = res.records || []
    }
  }
}
</script>

<style lang="scss" scoped>
.topsearch-container {
  display: flex;
  .item-height {
    height: 50px;
  }
  .topsearch-container-form {
    flex: 1;
  }
  .topsearch-height{
    height: 48px;
    overflow: hidden;
  }
  .topsearch-container-btn{
    width: 230px;
  }
}
</style>
